﻿using Autodesk.Revit.UI;
using Revit.Async;
using SEPD.CommonHelper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace SEPD.Template.Revit1
{
    public partial class TemplateViewModel
    {

        #region  revit相关操作

        /// <summary>
        /// 委托  用于承载实际的工作函数
        /// </summary>
        /// <param name="app"></param>
        public delegate void ExecuteDelegate(UIApplication app);

        /// <summary>
        /// revit async 测试样例
        /// </summary>
        async void RevitAsyncExample()
        {
            await RevitTask.RunAsync(
             app =>
             {
                 try
                 {
                     string rvtName = app.ActiveUIDocument.Document.PathName;
                     TaskDialog.Show("RVT-PATH:", rvtName);
                     //return null; 
                 }
                 catch (Exception exp)
                 {
                     ExceptionProcess.InfoProcess(
                         System.Reflection.MethodBase.GetCurrentMethod().DeclaringType,
                         exp.ToString());
                 }
             });
        }

        /// <summary>
        /// 接收委托的revit async 异步执行器
        /// </summary>
        /// <param name="executeDelegate"></param>
        async void RevitAsyncExecutor(ExecuteDelegate executeDelegate)
        {
            await RevitTask.RunAsync(app =>
            {
                try
                {
                    executeDelegate(app);
                }
                catch (Exception exp)
                {
                    ExceptionProcess.InfoProcess(MethodBase.GetCurrentMethod().DeclaringType, exp.ToString());
                }
            });
        }

        /// <summary>
        /// 接收委托的自定义 同步执行器
        /// </summary>
        void CommonSyncExecutor(ExecuteDelegate executeDelegate)
        {
            Action<UIApplication> actionX = new Action<UIApplication>(app =>
            {
                try
                {
                    executeDelegate(app);
                }
                catch (Exception exp)
                {
                    ExceptionProcess.InfoProcess(MethodBase.GetCurrentMethod().DeclaringType, exp.ToString());
                }
            });
            executeEventHandler.DelayRunAction(actionX);
        }

        #endregion

        #region 一般方法

        public delegate void CommonVoidDelegate();

        void CommonVoidExecutor(CommonVoidDelegate voidDelegate)
        {
            try
            {
                voidDelegate();
            }
            catch (Exception exp)
            {
                ExceptionProcess.InfoProcess(MethodBase.GetCurrentMethod().DeclaringType, exp.ToString());
            }
        }

        #endregion


    }

}
